#include "ModelMAYA.h"
#include <stdio.h>
#include <stdlib.h>
#include "../Str.h"
#include "../Logger.h"
#include <assert.h>

#define DEG 0
#define RAD 1
#define UNIT_CM 0

bool _ModelMAYA::load(char *filename)
{
	FILE *f;
	char tBuffer[1000];
	Str line;
	int unit=UNIT_CM;
	int degrad=DEG;
	Str node,lastNode;
	bool skip=false;
	double dotS[3]={1.0,1.0,1.0};//scale
	double dotT[3]={0.0,0.0,0.0};//translate
	double dotR[3]={0.0,0.0,0.0};//rotate
	int lineNum=0;//for debugging
	
	f=fopen(filename, "r");
	if (!f)
	{
		errLog.print(Str()<<"Could not open the Maya (ascii) file named \""<<filename<<"\"");
		return false;
	}
	while (!feof(f))
	{
		lineNum++;
		fgets(tBuffer, 1000, f);
		line = tBuffer;
		if (line.size()==0){continue;}
		//line.deleteRight(1);
		if (line.find("//")==0)
		{
			if (line.find("Codeset")>0)
			{
				line.keep(line.find("\"")+1,line.rfind("\""));
				int codeset=line.atoi();
				if (codeset!=1252)
				{
					errLog.print(Str()<<"Could not load in code set: "<<codeset);
					return 0;
				}
			}
		}
		else if (line.find("requires ")==0)
		{
			//nothing at this time
		}
		else if (line.find("currentUnit ")==0)
		{
			Str part;
			do
			{
				part.getFromMark(" ", line);
				if (line=="-l")
				{
					part.getFromMark(" ", line);
					if (part == "centimeter"){unit=UNIT_CM;mainLog.print("Unit stored as CM");}
					else{errLog.print(Str()<<"Unknown unit type: "<<part);}
				}
				else if (line=="-a")
				{
					part.getFromMark(" ", line);
					if (part == "degree"){degrad=DEG;mainLog.print("DEG");}
					else if (part == "radian"){degrad=RAD;mainLog.print("RAD");}
					else{errLog.print(Str()<<"Unknown arc type: "<<part);}
				}
			}while (line.size()>0);
		}
		else if (line.find("createNode ")==0)
		{
			Str part;
			bool didSkip;
			
			lastNode=node;
			didSkip=skip;
			skip=false;
			line.keep(11, line.rfind(";"));
			do {
				part.getFromMark(" ", line);
				//mainLog.print(part);
				if (part=="-n")
				{
					node.getFromMark(" ", line);//store the next value as the node name
					node.keep(1,-1);//remove quotes
				}
				else if (part=="camera" || part=="-s" || part=="script"
						 || part=="displayLayerManager" || part=="displayLayer"
						 || part=="renderLayerManager" || part=="renderLayer"
						 || part=="lightLinker"){skip=true;}//don't care about Maya's camera, etc
				else if (part=="-p"){skip=didSkip;}//if the node is a child of the camera etc, let's skip it as well
			} while (line.size()>0);
			if (!skip){mainLog.print(Str()<<"<<<\t"<<node<<"\t>>>");}
		}
		else if (line.getChar(0)=='\t')
		{
			if (skip){continue;}
			line.deleteLeft(1);
			Str part;
			if (line.find("setAttr ", 1)==0)
			{
				line.keep(9,0);
				part.getFromMark(" ", line);
				if (part == "\".t\"" || part == "\".r\"" || part == "\".s\"")
				{
					double *storage;
					if (part == "\".t\""){storage=dotT;}
					else if (part == "\".r\""){storage=dotR;}
					else if (part == "\".s\""){storage=dotS;}
					part.getFromMark(" ", line);//-type
					if (part!="-type")
					{
						//maya is just lazy sometimes
						//they just used one value to specify all 3
						double val=part.atod();
						storage[0]=val;
						storage[1]=val;
						storage[2]=val;
					}
					else
					{
						part.getFromMark(" ", line);
						part.keep(1,-1);
						if (part == "float3" || part == "double3")
						{
							part.getFromMark(" ", line);
							storage[0]=part.atod();
							part.getFromMark(" ", line);
							storage[1]=part.atod();
							part.getFromMark(" ", line);
							storage[2]=part.atod();
						}
						else{assert(!"WOAH THERE MAYA!");}
					}
				}
				else if (part.find("\".pt[")==0)
				{
					Str partb;
					int left,right;
					part.keep(5, 0);//remove ".pt[
					partb.getFromMark(":", part);
					left = partb.atoi();
					partb.getFromMark("]", part);
					right = partb.atoi();
					part.getFromMark(" ", line);
					if (part!="-type"){assert(!"WOAH THERE MAYA!");}
					part.getFromMark(" ", line);
					line.replaceAll("  "," ");
					//line.replaceAll("  "," ");
					//Mesh tMesh;
					//tMesh.triangleIndices
					if (part == "double3" || part == "float3")
					{
						//tMesh.
					}
					
				}
			}
		}
		else
		{
			//errLog.print("ELSE");
		}
	}
	mainLog.print(Str()<<"Units set to: "<<unit<<" and the arc unit is set to: "<<degrad);
	return true;
}

void _ModelMAYA::draw(int &index,double scale)
{
}